package com.itttq.sc.bspm.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.CachePut;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.itttq.sc.po.RiverPo;
import com.itttq.sc.service.RiverService;

@RestController
@RequestMapping("river")
public class RiverController {
	public static final String cacheKey = "r";
	@Autowired
	private RiverService riverService;
	@RequestMapping("/rivers")
	@Cacheable(value="rivers")
    public List<RiverPo> getRivers(RiverPo river) {
        return riverService.getRivers(river);
    }
	@RequestMapping("/{id}")
	@Cacheable(value=cacheKey, key="'river_'+#id")
	public RiverPo getRiverById(@PathVariable("id") int id) {
		return riverService.getRiver(id);
	}

	@RequestMapping("/save")
	@CachePut(value=cacheKey, key="'river_'+#river.id", condition="#river != null and #river.id != null")
	public void saveRiver(RiverPo river) {
		if(null == river) return;
		if(null == river.getId()) {
			riverService.addRiver(river);
		} else {
			riverService.updateRiver(river);
		}
	}

	@RequestMapping("/del/{id}")
	@CacheEvict(value=cacheKey, key="'river_'+#id", allEntries =true)
	public void delRiver(@PathVariable("id") int id) {
		riverService.delRiver(id);
	}
}
